home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / souper15.zip / REGEXP.C < prev    next >
C/C++ Source or Header  |  1996-05-18  |  29KB  |  1,234 lines

  1. /*
  2.  * regcomp and regexec -- regsub and regerror are elsewhere
  3.  * @(#)regexp.c    1.3 of 18 April 87
  4.  *
  5.  *    Copyright (c) 1986 by University of Toronto.
  6.  *    Written by Henry Spencer.  Not derived from licensed software.
  7.  *
  8.  *    Permission is granted to anyone to use this software for any
  9.  *    purpose on any computer system, and to redistribute it freely,
  10.  *    subject to the following restrictions:
  11.  *
  12.  *    1. The author is not responsible for the consequences of use of
  13.  *        this software, no matter how awful, even if they arise
  14.  *        from defects in it.
  15.  *
  16.  *    2. The origin of this software must not be misrepresented, either
  17.  *        by explicit claim or by omission.
  18.  *
  19.  *    3. Altered versions must be plainly marked as such, and must not
  20.  *        be misrepresented as being the original software.
  21.  *
  22.  * Beware that some of this code is subtly aware of the way operator
  23.  * precedence is structured in regular expressions.  Serious changes in
  24.  * regular-expression syntax might require a total rethink.
  25.  */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "regexp.h"
  29. #include "regmagic.h"
  30.  
  31. /*
  32.  * The "internal use only" fields in regexp.h are present to pass info from
  33.  * compile to execute that permits the execute phase to run lots faster on
  34.  * simple cases.  They are:
  35.  *
  36.  * regstart    char that must begin a match; '\0' if none obvious
  37.  * reganch    is the match anchored (at beginning-of-line only)?
  38.  * regmust    string (pointer into program) that match must include, or NULL
  39.  * regmlen    length of regmust string
  40.  *
  41.  * Regstart and reganch permit very fast decisions on suitable starting points
  42.  * for a match, cutting down the work a lot.  Regmust permits fast rejection
  43.  * of lines that cannot possibly match.  The regmust tests are costly enough
  44.  * that regcomp() supplies a regmust only if the r.e. contains something
  45.  * potentially expensive (at present, the only such thing detected is * or +
  46.  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
  47.  * supplied because the test in regexec() needs it and regcomp() is computing
  48.  * it anyway.
  49.  */
  50.  
  51. /*
  52.  * Structure for regexp "program".  This is essentially a linear encoding
  53.  * of a nondeterministic finite-state machine (aka syntax charts or
  54.  * "railroad normal form" in parsing technology).  Each node is an opcode
  55.  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
  56.  * all nodes except BRANCH implement concatenation; a "next" pointer with
  57.  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
  58.  * have one of the subtle syntax dependencies:  an individual BRANCH (as
  59.  * opposed to a collection of them) is never concatenated with anything
  60.  * because of operator precedence.)  The operand of some types of node is
  61.  * a literal string; for others, it is a node leading into a sub-FSM.  In
  62.  * particular, the operand of a BRANCH node is the first node of the branch.
  63.  * (NB this is *not* a tree structure:  the tail of the branch connects
  64.  * to the thing following the set of BRANCHes.)  The opcodes are:
  65.  */
  66.  
  67. /* definition    number    opnd?    meaning */
  68. #define    END    0    /* no    End of program. */
  69. #define    BOL    1    /* no    Match "" at beginning of line. */
  70. #define    EOL    2    /* no    Match "" at end of line. */
  71. #define    ANY    3    /* no    Match any one character. */
  72. #define    ANYOF    4    /* str    Match any character in this string. */
  73. #define    ANYBUT    5    /* str    Match any character not in this string. */
  74. #define    BRANCH    6    /* node    Match this alternative, or the next... */
  75. #define    BACK    7    /* no    Match "", "next" ptr points backward. */
  76. #define    EXACTLY    8    /* str    Match this string. */
  77. #define    NOTHING    9    /* no    Match empty string. */
  78. #define    STAR    10    /* node    Match this (simple) thing 0 or more times. */
  79. #define    PLUS    11    /* node    Match this (simple) thing 1 or more times. */
  80. #define    OPEN    20    /* no    Mark this point in input as start of #n. */
  81.             /*    OPEN+1 is number 1, etc. */
  82. #define    CLOSE    30    /* no    Analogous to OPEN. */
  83.  
  84. /*
  85.  * Opcode notes:
  86.  *
  87.  * BRANCH    The set of branches constituting a single choice are hooked
  88.  *        together with their "next" pointers, since precedence prevents
  89.  *        anything being concatenated to any individual branch.  The
  90.  *        "next" pointer of the last BRANCH in a choice points to the
  91.  *        thing following the whole choice.  This is also where the
  92.  *        final "next" pointer of each individual branch points; each
  93.  *        branch starts with the operand node of a BRANCH node.
  94.  *
  95.  * BACK        Normal "next" pointers all implicitly point forward; BACK
  96.  *        exists to make loop structures possible.
  97.  *
  98.  * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
  99.  *        BRANCH structures using BACK.  Simple cases (one character
  100.  *        per match) are implemented with STAR and PLUS for speed
  101.  *        and to minimize recursive plunges.
  102.  *
  103.  * OPEN,CLOSE    ...are numbered at compile time.
  104.  */
  105.  
  106. /*
  107.  * A node is one char of opcode followed by two chars of "next" pointer.
  108.  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
  109.  * value is a positive offset from the opcode of the node containing it.
  110.  * An operand, if any, simply follows the node.  (Note that much of the
  111.  * code generation knows about this implicit relationship.)
  112.  *
  113.  * Using two bytes for the "next" pointer is vast overkill for most things,
  114.  * but allows patterns to get big without disasters.
  115.  */
  116. #define    OP(p)    (*(p))
  117. #define    NEXT(p)    (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
  118. #define    OPERAND(p)    ((p) + 3)
  119.  
  120. /*
  121.  * See regmagic.h for one further detail of program structure.
  122.  */
  123.  
  124.  
  125. /*
  126.  * Utility definitions.
  127.  */
  128. #ifndef CHARBITS
  129. #define    UCHARAT(p)    ((int)*(unsigned char *)(p))
  130. #else
  131. #define    UCHARAT(p)    ((int)*(p)&CHARBITS)
  132. #endif
  133.  
  134. #define    FAIL(m)    { regerror(m); return(NULL); }
  135. #define    ISMULT(c)    ((c) == '*' || (c) == '+' || (c) == '?')
  136. #define    META    "^$.[()|?+*\\"
  137.  
  138. /*
  139.  * Flags to be passed up and down.
  140.  */
  141. #define    HASWIDTH    01    /* Known never to match null string. */
  142. #define    SIMPLE        02    /* Simple enough to be STAR/PLUS operand. */
  143. #define    SPSTART        04    /* Starts with * or +. */
  144. #define    WORST        0    /* Worst case. */
  145.  
  146. /*
  147.  * Global work variables for regcomp().
  148.  */
  149. static char *regparse;        /* Input-scan pointer. */
  150. static int regnpar;        /* () count. */
  151. static char regdummy;
  152. static char *regcode;        /* Code-emit pointer; ®dummy = don't. */
  153. static long regsize;        /* Code size. */
  154.  
  155. /*
  156.  * Forward declarations for regcomp()'s friends.
  157.  */
  158. #ifdef __WIN32__
  159. /* Some prototypes to shut the compiler up */
  160. regexp *regcomp(char *exp);
  161. static int regmatch(char *prog);
  162. static int regtry(regexp *prog, char *string);
  163. static char *regnext(register char *p);
  164. static char *regatom(int *flagp);
  165. static char *regnode(char op);
  166. static void regtail(char *p, char *val);
  167. static void regc(char b);
  168. static char *reg(int paren, int *flagp);
  169. static void reginsert(char op, char *opnd);
  170. static void regoptail(char *p, char *val);
  171. static char *regbranch(int *flagp);
  172. static char *regpiece(int *flagp);
  173. void regerror (const char *msg);
  174. #else
  175.  
  176. #ifndef STATIC
  177. #define    STATIC    static
  178. #endif
  179. STATIC char *reg();
  180. STATIC char *regbranch();
  181. STATIC char *regpiece();
  182. STATIC char *regatom();
  183. STATIC char *regnode();
  184. STATIC char *regnext();
  185. STATIC void regc();
  186. STATIC void reginsert();
  187. STATIC void regtail();
  188. STATIC void regoptail();
  189. #ifdef STRCSPN
  190. STATIC int strcspn();
  191. #endif
  192.  
  193. #endif
  194.  
  195. /*
  196.  - regcomp - compile a regular expression into internal code
  197.  *
  198.  * We can't allocate space until we know how big the compiled form will be,
  199.  * but we can't compile it (and thus know how big it is) until we've got a
  200.  * place to put the code.  So we cheat:  we compile it twice, once with code
  201.  * generation turned off and size counting turned on, and once "for real".
  202.  * This also means that we don't allocate space until we are sure that the
  203.  * thing really will compile successfully, and we never have to move the
  204.  * code and thus invalidate pointers into it.  (Note that it has to be in
  205.  * one piece because free() must be able to free it all.)
  206.  *
  207.  * Beware that the optimization-preparation code in here knows about some
  208.  * of the structure of the compiled regexp.
  209.  */
  210. regexp *
  211. regcomp(exp)
  212. char